import 'dart:async';

import 'package:flutter/services.dart';

final hyoukaAudioRecord = HyoukaAudioRecord._();

class HyoukaAudioRecord {
  static final _instance = HyoukaAudioRecord._interval();

  late final EventChannel _eventChannel;

  factory HyoukaAudioRecord._() => _instance;

  HyoukaAudioRecord._interval() {
    final channelName = "cn.cjlmonster.hyouka/audio";
    _eventChannel = EventChannel(channelName);
  }

  StreamSubscription? _streamSubscription;

  Future<void> startRecord(void Function(dynamic) listen, {int sampleRate = 44100}) async {
    _streamSubscription?.cancel();
    _streamSubscription = _eventChannel.receiveBroadcastStream({"sampleRate": sampleRate}).listen(listen);
  }

  Future<void> stopRecord() async {
    _streamSubscription?.cancel();
    _streamSubscription = null;
  }
}
